<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Requests\AdminLoopRequest;
use App\Http\Requests\AdminLoopUpdate;
use App\Http\Controllers\Api\CommonApi;
use Illuminate\Support\Facades\Redis;
use App\Model\AdminLoopModel;

class AdminLoop extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        $loops =  AdminLoopModel::select('id', 'goods_id', 'name', 'loop_pic', 'status', 'updated_at')->get();


       return view('Admin.looppic.picture-list', ['loops' => $loops]);
        
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Request $request)
    {
        //
        //
        return view('Admin.looppic.picture-add', ['goods_id' => $request->id]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(AdminLoopRequest $request)
    {
        //
        
        $index = new CommonApi;

        $respic = $index->uploadToQiNiu($request, 'loop_pic');

        if ($respic['status'] == 2200 ) {
            
            $loop = new AdminLoopModel;

            $loop->goods_id = $request->input('goods_id');

            $loop->name = $request->input('name');

            $loop->loop_pic = $respic['data']['key'];

            $loop->save();

          return back()->with('message', '添加成功');
        }

        return back()->with('message', '轮播图上传失败！');
         
    }

    public function stop(Request $request)
    {

        $res = AdminLoopModel::where('id', $request->id)->update(['status' => 0]);

        //下架就把Redis里的轮播图删掉
        $setKey = 'shop_looppics_set_data';

        $hashKey = 'shop_looppics_hash_data:'.$request->id;

        Redis::sRem($setKey, $hashKey);

        Redis::del($hashKey);

        return response()->json($res);

    }

    public function start(Request $request)
    {

        $res = AdminLoopModel::where('id', $request->id)->update(['status' => 1]);

        $arr = AdminLoopModel::select('goods_id', 'loop_pic')->where('id', $request->id)->get()->first()->toArray();

        //将轮播图加入到redis中
        $setKey = 'shop_looppics_set_data';

        $hashKey = 'shop_looppics_hash_data:'.$request->id;

        Redis::sAdd($setKey, $hashKey);

        Redis::hMSet($hashKey, $arr);

        return response()->json($res);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
        $loops = AdminLoopModel::find($id);
        return view('Admin.looppic.picture-edit', ['loops' => $loops]);
        
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(AdminLoopUpdate $request, $id)
    {
        //
        //
        $pics = AdminLoopModel::find($id);         

        if ($request->file('loop_pic')) {

            $common = new CommonApi;

            $res = $common->uploadToQiNiu($request, 'loop_pic');
            //判断是否上传成功
            if ( $res['status'] == 2200 ) {

                $pics->loop_pic = $res['data']['key'];

                $common->deleteToQiNiu('shop_looppics', 'loop_pic', $id);

            } else {

                return back()->with('message', '图片上传失败');
            }


        }

        $pics->name = $request->input('name');

        $pics->update();

        return redirect()->route('AdminLoop.index')->with('message', '编辑成功');
     
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy(Request $request)
    {
        $status = AdminLoopModel::select('status')->where('id', $request->id)->first();
        
        if($status->status == 1) {

            return response()->json(['message' => '不能删除发布中的轮播图']);

        } else {

            $common = new CommonApi;

            $common->deleteToQiNiu('shop_looppics', 'loop_pic', $request->id);

            $res = AdminLoopModel::where('id', $request->id)->delete();

            return response()->json(['message' => '删除成功']);

        }


    }
}
